<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>Comet echo RPC</title>

    <script type="text/javascript">
        var djConfig = { isDebug: false };
    </script>

    <script type="text/javascript" src="${pageContext.request.contextPath}/js/dojo/dojo.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/prototype.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/behaviour.js"></script>

    <script language="JavaScript" type="text/javascript">
        var EvUtil = {
            getKeyCode : function(ev) {
                if (window.event) {
                    return window.event.keyCode;
                } else {
                    return ev.keyCode;
                }
            }
        };

        var echoBehaviours = {
            '#phrase' : function(element) {
                element.setAttribute("autocomplete", "OFF");
                element.onkeyup = function(ev) {
                    var keyc = EvUtil.getKeyCode(ev);
                    if (keyc == 13 || keyc == 10) {
                        echoRpc($('phrase').value);
                        $('phrase').value = '';
                        return false;
                    }
                    return true;
                }
            },
            '#sendB' : function(element) {
                element.onclick = function(event) {
                    echoRpc($('phrase').value);
                    $('phrase').value = '';
                    return false;
                }
            }
        };

        function echoRpc(msg) {
            dojox.cometd.publish("/rpc/hello", msg);
        }

        Behaviour.register(echoBehaviours);

        function echoRpcReturn(msg) {
            $("responses").innerHTML += msg.timestamp + " " + msg.channel + ": " + msg.data + "<br/>";
        }

        dojo.require("dojox.cometd");
        dojo.connect(dojox.cometd, "finishInit",
            function(type, data, evt, request) {
                dojox.cometd.subscribe("/rpc/hello/" + dojox.cometd.clientId, "echoRpcReturn");
            }
        );
        dojox.cometd.init("${pageContext.request.contextPath}/cometd");
    </script>
</head>
<body>

<h1>Hello Test (Spring Enabled)</h1>

<p>This example shows that BayeuxMessageHandler are first class Spring beans that can be configured in the usual way. In
    this
    case the HelloMessageHandler is injected with a reference to the HelloService bean which is responsible for
    generating the responses.</p>

<div>Your name:&nbsp;<input id="phrase" type="text"></input> <input id="sendB" class="button" type="submit" name="join"
                                                                    value="Send"/>
</div>
<pre id="responses"></pre>

<h3>Java Code</h3>

    <pre>
        public class HelloMessageHandler implements BayeuxMessageHandler
        {
            private HelloService helloService;

            @Required
            public void setHelloService(HelloService helloService) {
                this.helloService = helloService;
            }

            public void handle(Bayeux bayeux, Channel channel, Client client, Object data) {
                Channel replyChannel = bayeux.getChannel(channel.getId() + "/" + client.getId());
                if (replyChannel != null) {
                    replyChannel.publish(helloService.sayHello(data.toString()), client);
                }
            }
        }
    </pre>

<h3>Spring Configuration</h3>

    <pre>
        &lt;bean id="helloService" class="comet.helloworld.spring.HelloServiceImpl"/>

        &lt;bean id="helloDataFilter" class="comet.helloworld.spring.BayeuxDataFilterContainer">
            &lt;property name="clientName" value="helloServiceClient"/>
            &lt;property name="channelName" value="/helloService"/>
            &lt;property name="filter">
                &lt;bean class="comet.helloworld.spring.HelloServiceDataFilter">
                    &lt;property name="helloService" ref="helloService"/>
                &lt;/bean>
            &lt;/property>
        &lt;/bean>
    </pre>

</body>
</html>
